package edu.iss.demo.service;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.iss.demo.exception.PublicHolidayNotFound;
import edu.iss.demo.model.PublicHoliday;
import edu.iss.demo.repository.PublicHolidayRepository;
@Service
public class PublicHolidayServiceImpl implements PublicHolidayService {

	
	@Resource
	PublicHolidayRepository publicHolidayRepository;
	
	@PersistenceContext
	EntityManager entityManager;
	

	@Override
	@Transactional
	public PublicHoliday create(PublicHoliday holiday) {
		PublicHoliday publicHoliday = holiday;
		return publicHolidayRepository.save(publicHoliday);
	}

	@Override
	@Transactional(rollbackFor = PublicHolidayNotFound.class)
	public PublicHoliday delete(long id) throws PublicHolidayNotFound{
		PublicHoliday deletedPublicHoliday= publicHolidayRepository.findOne(id);

		if (deletedPublicHoliday== null)
			throw new PublicHolidayNotFound();

		deletedPublicHoliday.setDeleteFlag(true);
		publicHolidayRepository.save(deletedPublicHoliday);
		return deletedPublicHoliday;
	}

	@Override
	@Transactional
	public List<PublicHoliday> findAll() {
		
		return publicHolidayRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor = PublicHolidayNotFound.class)
	public PublicHoliday update(PublicHoliday s) throws PublicHolidayNotFound{
		PublicHoliday updatedPublicHoliday= publicHolidayRepository.findOne(s.getPublicHolidayid());

		if (updatedPublicHoliday == null)
			throw new PublicHolidayNotFound();

		updatedPublicHoliday.setDate(s.getDate());
		updatedPublicHoliday.setUserId(s.getUserId());
		
		publicHolidayRepository.flush();
		return updatedPublicHoliday;
	}

	@Override
	@Transactional
	public PublicHoliday findById(long id) {
		return publicHolidayRepository.findOne(id);
	}

}
